package com.doform.android.xianggucheng.utils;

import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.text.TextUtils;
import android.view.View;
import android.widget.TextView;

import com.bigkoo.pickerview.TimePickerView;
import com.doform.android.xianggucheng.IApplication;
import com.doform.android.xianggucheng.help.AppHelp;

import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Map;

/**
 * Created by Administrator on 2017/5/12.
 */

public class AppUtils {

    public static final String TAG = "apen";

    public static String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    public static Context getContext() {
        return IApplication.getInstance();
    }

    public static TimePickerView getTimePicker() {
        //控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        startDate.set(2013, 0, 23);

        Calendar endDate = Calendar.getInstance();
        endDate.set(2019, 11, 28);
        //时间选择器
        TimePickerView pvTime = new TimePickerView.Builder(getContext(), new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                // 这里回调过来的v,就是show()方法里面所添加的 View 参数，如果show的时候没有添加参数，v则为null

                /*btn_Time.setText(getTime(date));*/
                TextView btn = (TextView) v;
                btn.setText(getTime(date));
            }
        })
                .setType(TimePickerView.Type.YEAR_MONTH_DAY)
                .setLabel("", "", "", "", "", "") //设置空字符串以隐藏单位提示   hide label
                .setDividerColor(Color.DKGRAY)
                .setContentSize(20)
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .build();

        return pvTime;
    }


    public static void setHttpHead(Map<String, String> map) {
        /**
         *     * _v	√	1.0
         * _r	√	{andorid | weixin }
         * _n	√	base.clazz
         */
        map.put("_v", "1.0");
        map.put("_r", "andorid");

        map.put("_username", AppHelp.getInstance().getUserName());
        map.put("_password", AppHelp.getInstance().getPassword());

    }


    /**
     * 合并Url&Params
     *
     * @param url       url
     * @param urlParams urlParams
     * @return merged
     */
    public static String merge(String url, Map<String, String> urlParams, boolean isUrlEncode) {

        StringBuffer sb = new StringBuffer(url);

        if (urlParams != null) {

            String params = getUrlParams(urlParams, isUrlEncode);

            if (!TextUtils.isEmpty(params)) {

                if (sb.indexOf("?") != -1) {
                    // 如果URL中包含参数
                    sb.append("&");
                } else {
                    // 如果不包含参数
                    sb.append("?");
                }

                sb.append(params);
            }
        }

        return sb.toString();
    }

    /**
     * 按照URL解析拼接字符串为URL参数
     *
     * @param paramsMap 字符串参数
     * @return 拼接后的URL参数
     */
    private static String getUrlParams(Map<String, String> paramsMap, boolean isUrlEncode) {

        if (paramsMap == null) {
            return "";
        }

        if (paramsMap.size() <= 0) {
            return "";
        }

        StringBuffer sb = new StringBuffer();

        for (Map.Entry<String, String> entry : paramsMap.entrySet()) {

            String key = entry.getKey();
            String value = entry.getValue();

            if (key == null
                    || value == null) {
                continue;
            }

            if (isUrlEncode) {
                key = URLEncoder.encode(key);
                value = URLEncoder.encode(value);
            }

            // 按照URL参数规则拼接字符串
            sb.append(String.format("%s=%s&", key, value));
        }

        if (sb.length() <= 0) {
            return "";
        } else {
            return sb.substring(0, sb.length() - 1);
        }
    }

    //价钱转换
    public static String convertPrice(String stringExtraPrice) {

        Double doublePrice = Double.valueOf(stringExtraPrice);

        int doubleFen = (int) (doublePrice * 100);

        String strFen = String.valueOf(doubleFen);

        int leng = 12 - strFen.length();

        String str = "";

        for (int i = 0; i < leng; i++) {
            str = str + 0;
        }

        String price = str + strFen;

        return price;
    }

    public static int getVersionCode(Context mContext) {
        if (mContext != null) {
            try {
                return mContext.getPackageManager().getPackageInfo(mContext.getPackageName(), 0).versionCode;
            } catch (PackageManager.NameNotFoundException ignored) {
            }
        }
        return 0;
    }

    public static boolean isContextValid(Context context) {
        return context instanceof Activity && !((Activity) context).isFinishing();
    }

    public static Drawable resourcesTodrawable(Context mContext, int drawable) {
        Bitmap bitmap = BitmapFactory.decodeResource(mContext.getResources(),drawable);
        return new BitmapDrawable(mContext.getResources(),bitmap);
    }
}
